package net.xumf.support.security;

import net.xumf.support.consts.Constants.Normal;
import net.xumf.support.utils.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.util.DigestUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.UUID;

/**
 * 密码规制
 */
public final class PasswordService {

    /**
     * 生成盐值
     *
     * @return 盐值
     */
    public static int constructSalt() {
        return CommonUtil.getSixRandomNumber();
    }

    /**
     * 构造加密密码
     *
     * @param password 密码
     * @param salt     盐值
     * @return 加密密码
     */
    public static String encryptPassword(String password, int salt) {
        return DigestUtils.md5DigestAsHex((password + salt).getBytes());
    }

    public static void main(String[] args) {
//        b0b175013c2b59090969d78b8ed3641891254cdc
        String pw = "pass1234";
        String s = encryptPassword(pw, 229822);
        System.out.println(s);
    }

    /**
     * 获取前端aes key（长度16）
     *
     * @return key
     */
    public static String constructAesKey() {

        UUID uuid = UUID.randomUUID();
        String[] strings = uuid.toString().split("-");
        return Arrays.stream(strings).limit(3).reduce((a, b) -> a + b).orElse("8f4955c0976f4f5d");
    }

    /**
     * 获取aes解密后的密码
     *
     * @param afterEncryptionPassword aes加密后的密码
     * @param key                     aes解密键
     * @return 解密后的密码
     * @throws Exception 解密异常
     */
    public static String decryptAesPassword(String afterEncryptionPassword, String key) throws Exception {
        return EncryptUtils.aesDecrypt(afterEncryptionPassword, key);
    }

    /**
     * 编码工具类
     * 实现aes加密、解密
     */
    public static final class EncryptUtils {

        /**
         * master 64 encode
         *
         * @param bytes 待编码的byte[]
         * @return 编码后的base 64 code
         */
        static String base64Encode(byte[] bytes) {
            return Base64.encodeBase64String(bytes);
        }

        /**
         * master 64 decode
         *
         * @param base64Code 待解码的base 64 code
         * @return 解码后的byte[]
         * @throws Exception
         */
        static byte[] base64Decode(String base64Code) throws Exception {
            return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
        }


        /**
         * AES加密
         *
         * @param content    待加密的内容
         * @param encryptKey 加密密钥
         * @return 加密后的byte[]
         * @throws Exception
         */
        static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
            KeyGenerator kgen = KeyGenerator.getInstance(Normal.AES_ALGORITHM_NAME);
            kgen.init(128);
            Cipher cipher = Cipher.getInstance(Normal.ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), Normal.AES_ALGORITHM_NAME));

            return cipher.doFinal(content.getBytes("utf-8"));
        }


        /**
         * AES加密为base 64 code
         *
         * @param content    待加密的内容
         * @param encryptKey 加密密钥
         * @return 加密后的base 64 code
         * @throws Exception {@link Exception}
         */
        static String aesEncrypt(String content, String encryptKey) throws Exception {
            return base64Encode(aesEncryptToBytes(content, encryptKey));
        }

        /**
         * AES解密
         *
         * @param encryptBytes 待解密的byte[]
         * @param decryptKey   解密密钥
         * @return 解密后的String
         * @throws Exception
         */
        static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
            KeyGenerator ken = KeyGenerator.getInstance(Normal.AES_ALGORITHM_NAME);
            ken.init(128);
            Cipher cipher = Cipher.getInstance(Normal.ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), Normal.AES_ALGORITHM_NAME));
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
            return new String(decryptBytes);
        }


        /**
         * 将base 64 code AES解密
         *
         * @param encryptStr 待解密的base 64 code
         * @param decryptKey 解密密钥
         * @return 解密后的string
         * @throws Exception
         */
        static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
            return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
        }

    }


}
